const assert = require('assert');
const { getSocketByHotelId, remote } = require('../utils');

class LocalInterface {
  async getData(req, res, next) {
    try {
      console.log('socket.io ', 'get-data route');
      assert(req.query.hotelId, '无效的酒店id');
      const socket = await getSocketByHotelId(req.query.hotelId);
      const data = await remote(socket, req);
      res.send(data);
    }
    catch (e) {
      next(e);
    }
  }

  async setData(req, res, next) {
    try {
      console.log('socket.io ', 'set-data route');
      assert(req.body.hotelId, '无效的酒店id');
      const socket = await getSocketByHotelId(req.body.hotelId);
      const data = await remote(socket, req);
      res.send(data);
    }
    catch (e) {
      next(e);
    }
  }

  async sendToRemote(req, res, next) {
    try {
      const socket = await getSocketByHotelId(req.params.hotelId);
      const data = await remote(socket, req);
      res.send(data);
    }
    catch (e) {
      next(e);
    }
  }
}

module.exports = new LocalInterface();
